<template>
  <div>
    <el-dialog :visible="isShow" title="权限管理" @close="closeEvent">
      <el-tree
        v-if="bol"
        ref="tree"
        :data="List"
        :props="{ label: 'name' }"
        check-strictly
        show-checkbox
        node-key="id"
        :default-checked-keys="defaultkeys"
      />
      <template #footer>
        <el-button type="primary" @click="submit">确定</el-button>
        <el-button @click="closeEvent">取消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { changeData } from '@/utils/index'
import { sysPermission } from '@/api/permission'
import { sysRoleById, sysRoleAssignPrem } from '@/api/setting'
export default {
  props: {
    isShow: {
      type: Boolean,
      default: false,
      required: true
    }
  },
  data() {
    return {
      defaultkeys: [],
      bol: true,
      id: '',
      List: []
    }
  },
  created() {
    this.getData()
  },
  methods: {
    // 关闭组件时触发
    closeEvent() {
      this.$emit('update:isShow', false)
      // 静默刷新
      this.defaultkeys = []
      this.bol = false
      this.$nextTick(() => {
        this.bol = true
      })
    },
    // 获取list数据
    async getData() {
      const res = await sysPermission()
      this.List = changeData(res.data, '0')
      console.log(res)
    },
    // 定义初始选中选项
    async getPermission(row) {
      this.id = row.id
      const res = await sysRoleById(row.id)
      this.$refs.tree.setCheckedKeys(res.data.permIds)
      console.log(res)
    },
    // 提交数据时触发
    async submit() {
      await sysRoleAssignPrem(this.id, this.$refs.tree.getCheckedKeys())
      this.$message.success('数据更新成功')
      this.closeEvent()
    }
  }
}
</script>

<style></style>
